package com.ruoyi.worker.service.impl;

import com.google.common.collect.Lists;
import com.ruoyi.common.constant.AttendanceConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.worker.domain.*;
import com.ruoyi.worker.mapper.AttendanceStatisticsMapper;
import com.ruoyi.worker.service.AttendanceStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName: AttendanceStatisticsServiceImpl
 * @Description:
 * @Author: 刘松山
 * @Date: 2021/10/18 14:23
 */
@Service
public class AttendanceStatisticsServiceImpl implements AttendanceStatisticsService {

    @Autowired
    private AttendanceStatisticsMapper attendanceStatisticsMapper;

    @Override
    public List<UserDayAttendance> listUserDayAttendance(UserDayAttendance u) {
        List<UserDayAttendance> list = attendanceStatisticsMapper.listUserDayAttendance(u);
        for (UserDayAttendance uda : list) {
            if (uda.getFirstStatus() != null) {
                uda.setShowFirstStatus(uda.getFirstStatus() + "/" + uda.getFiveStatus());
            }
            if (uda.getSecondStatus() != null) {
                uda.setShowSecondStatus(uda.getSecondStatus() + "/" + uda.getSixStatus());
            }
            if (uda.getThreeStatus() != null) {
                uda.setShowThreeStatus(uda.getThreeStatus() + "/" + uda.getSevenStatus());
            }
            if (uda.getFourStatus() != null) {
                uda.setShowFourStatus(uda.getFourStatus() + "/" + uda.getEightStatus());
            }


        }

        return list;
    }

    @Override
    public List<UserDayAttendanceSum> listUserDayAttendanceSum(UserDayAttendanceSum u) {
        return attendanceStatisticsMapper.listUserDayAttendanceSum(u);
    }

    @Override
    public List<UserMonthAttendance> listUserMonthAttendance(UserMonthAttendance u) {

        List<UserMonthAttendance> list = attendanceStatisticsMapper.listUserMonthAttendance(u);
        String currentDate = DateUtils.getDate();
        int month = 0;
        long  currentDateValue =Long.parseLong(currentDate.substring(0,7).replaceAll("-",""));
        if(currentDateValue>Long.parseLong(u.getDateTime().replaceAll("-",""))){
            month =Integer.parseInt(u.getDateTime().substring(5, 7));
            currentDate=DateUtils.getLastDayOfMonth(month);
        }else{
            month =Integer.parseInt(currentDate.substring(5, 7));
        }


        String firstDay = DateUtils.getFirstDayOfMonth(month);
        List<String> dateArray = null;
        try {
            dateArray = DateUtils.findDates(firstDay, currentDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        List<String> userList = new ArrayList<>();
        for (UserMonthAttendance uma : list) {
            userList.add(uma.getUserId() + "");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("list", userList);
        map.put("groupId", u.getGroupId());
        map.put("dateTime", currentDate);
        List<Map<String, Object>> userScheduleList = attendanceStatisticsMapper.listUserScheduleTimeShif(map);
        for (UserMonthAttendance uma : list) {
            List<Map<String, Object>> userTypeList = new ArrayList<>();
            for (Map<String, Object> m : userScheduleList) {

                Map<String, Object> resultMap = new HashMap<>();
                if (Long.parseLong(m.get("userId") + "") == uma.getUserId()) {
                    resultMap.put(m.get("dateTime")+"", m.get("dateTime"));
                    resultMap.put("type",m.get("normalNum")+"/"+Integer.parseInt(m.get("type")+"") *2);

                    userTypeList.add(resultMap);
                    continue;
                }
            }
            uma.setDataList(userTypeList);
        }

        for (UserMonthAttendance uma : list) {
            List<Map<String, Object>> userTypeList =uma.getDataList();
            for (String s:dateArray) {

              int flag =0;
                for(Map<String, Object> m:userTypeList) {
                    if (m.get(s) != null) {
                        flag = 1;
                        m.put("dateTime",s);
                        break;
                    }
                }
                if(flag !=1){
                    Map<String, Object> resultMap = new HashMap<>();
                    resultMap.put("dateTime",s);
                    resultMap.put("type", "0/0");
                    userTypeList.add(resultMap);

                }
            }
            uma.setDataList(userTypeList);
        }

        return list;
    }

    @Override
    public List<UserMonthAttendanceSum> listUserMonthAttendanceSum(UserMonthAttendanceSum u) {
        return attendanceStatisticsMapper.listUserMonthAttendanceSum(u);
    }

    @Override
    public List<Map<String, Object>> listGroupByUserName(Map<String, Object> map) {
        return attendanceStatisticsMapper.listGroupByUserName(map);
    }

    @Override
    public List<Map<String, Object>> listRestSchedule(String dateTime) {
        return attendanceStatisticsMapper.listRestSchedule(dateTime);
    }

    @Override
    public List<UserDayAttendance> listUserScheduleInfo(Map<String, Object> map) {
        return attendanceStatisticsMapper.listUserScheduleInfo(map);
    }

    @Override
    @Transactional
    public int addUserDayAttendanceSum(Map<String, Object> map) {

        List<UserDayAttendanceSum> uList = attendanceStatisticsMapper.getUserDayAttendanceSum(map.get("dateTime") + "");
        attendanceStatisticsMapper.delUserDayAttendanceSum(map.get("dateTime") + "");
        for (UserDayAttendanceSum u : uList) {
            if (u.getId() != 0l) {
                attendanceStatisticsMapper.delUserDayAttendance(u.getId());

            }
        }


        List<UserDayAttendanceSum> userDayAttendanceSumList = (List<UserDayAttendanceSum>) map.get("userDayAttendanceSumList");


        for (UserDayAttendanceSum uas : userDayAttendanceSumList) {
            if(uas.getUserDayAttendanceList().size()>0){
                attendanceStatisticsMapper.addUserDayAttendanceSum(uas);
                List<UserDayAttendance> list = uas.getUserDayAttendanceList();
                for (UserDayAttendance userDayAttendance : list) {
                    userDayAttendance.setParentId(uas.getId());
                }
                attendanceStatisticsMapper.addUserDayAttendance(list);
            }

        }


        return 1;
    }

    @Override
    public List<Map<String, Object>> listUserSchedule(String dateTime) {
        return attendanceStatisticsMapper.listUserSchedule(dateTime);
    }


        
    public AjaxResult dayAttendanceImport(List<MonthlyAttendanceIni> iniList,String username) {
       
        MonthlyAttendanceIni fistRecord = iniList.get(0);
        String fistRecordDateTime = fistRecord.getDateTime();
        String date = DateUtils.parseStrToStr("yyyy-MM-dd", fistRecordDateTime);
        Set<String> userSet = new HashSet<>();
        for (MonthlyAttendanceIni m : iniList) {

            if (DateUtils.parseStrToStr("yyyy-MM-dd", m.getDateTime()).equals(date)) {
                userSet.add(m.getUserName());
            }
        }
        List<String> userList = new ArrayList<>(userSet);
        Map<String, Object> paraMap = new HashMap<>();
        paraMap.put("dateTime", fistRecordDateTime);
        paraMap.put("list", userList);
        List<Map<String, Object>> groupList = attendanceStatisticsMapper.listGroupByUserName(paraMap);


        List<UserDayAttendance> scheduleList = attendanceStatisticsMapper.listUserScheduleInfo(paraMap);
        if (scheduleList.size() < 1) {
            return AjaxResult.error(date + "没有排班信息，请核对！" + "");
        }


        //获取考勤组下数据列表
        List<List<UserDayAttendance>> groupOfList = Lists.newArrayList();
        scheduleList.stream().collect(Collectors.groupingBy(UserDayAttendance::getGroupId, Collectors.toList()))
                .forEach((groupId, templist) -> {
                    groupOfList.add(templist);
                });


        //获取所有用户列表
        List<List<MonthlyAttendanceIni>> userOfList = Lists.newArrayList();
        iniList.stream().collect(Collectors.groupingBy(MonthlyAttendanceIni::getUserCode, Collectors.toList()))
                .forEach((userCode, templist) -> {
                    userOfList.add(templist);
                });


        //获取当天所有用户的排班记录列表

        List<Map<String, Object>> userScheduleList = attendanceStatisticsMapper.listUserSchedule(fistRecordDateTime);


        Date dateTime = DateUtils.dateTime("yyyy-MM-dd HH:mm", fistRecordDateTime);
        String baseDate = DateUtils.parseStrToStr("yyyy-MM-dd", fistRecordDateTime);

        List<UserDayAttendance> resultList = new ArrayList<>();
        Map<String, Object> resultMap = new HashMap<>();


        Date date1 = null;
        Date date2 = null;
        Date date3 = null;
        Date date4 = null;
        Date date5 = null;
        Date date6 = null;
        Date date7 = null;
        Date date8 = null;

        //应该出勤的人数
        int mustCome = 0;

        List<Map<String, Object>> collectList = new ArrayList<>();
        for (int i = 0; i < userScheduleList.size(); i++) {
            Map<String, Object> map = userScheduleList.get(i);

            String userName = map.get("userName") + "";
            for (List<MonthlyAttendanceIni> l : userOfList) {
                if (userName.equals(l.get(0).getUserName())) {
                    map.put("userList", l);
                    collectList.add(map);
                    continue;
                }
            }
        }

        List<Map<String, Object>> restList = new ArrayList<>();
        //循环合并后的列表 对比落入通行记录里 有 正常  没有 异常
        for (int i = 0; i < collectList.size(); i++) {
            Map<String, Object> map = collectList.get(i);
            List<MonthlyAttendanceIni> l = (List<MonthlyAttendanceIni>) collectList.get(i).get("userList");
            if ("休息".equals(map.get("name") + "")) {
                UserDayAttendance userDayAttendance = new UserDayAttendance();
                userDayAttendance.setIsAttendance(0);
                userDayAttendance.setUserId(Long.parseLong(map.get("userId") + ""));
                userDayAttendance.setUserName(map.get("userName") + "");
                userDayAttendance.setIsCome(0);
                userDayAttendance.setIsAbnormal(0);
                userDayAttendance.setCreateBy(username);
                userDayAttendance.setPostId(Long.parseLong(map.get("postId") + ""));
                userDayAttendance.setPostName(map.get("postName") + "");
                userDayAttendance.setDateTime(DateUtils.parseDateToStr("yyyy-MM-dd", dateTime));
                userDayAttendance.setIdCard(l.get(0).getIdCard());
                resultList.add(userDayAttendance);
                Map<String, Object> restMap = new HashMap<>();
                restMap.put("", map.get("groupId"));

                restList.add(restMap);
                continue;
            }
            mustCome++;
            //一个长期工一天一条记录
            UserDayAttendance userDayAttendance = new UserDayAttendance();
            userDayAttendance.setGroupId(Long.parseLong(map.get("groupId") + ""));
            userDayAttendance.setUserId(Long.parseLong(map.get("userId") + ""));
            userDayAttendance.setUserName(map.get("userName") + "");
            userDayAttendance.setIdCard(l.get(0).getIdCard());
            userDayAttendance.setCreateBy(username);
            userDayAttendance.setPostId(Long.parseLong(map.get("postId") + ""));
            userDayAttendance.setPostName(map.get("postName") + "");
            userDayAttendance.setIsAbnormal(0);
            userDayAttendance.setDateTime(DateUtils.parseDateToStr("yyyy-MM-dd", dateTime));
            int type = Integer.parseInt(map.get("type") + "");

            userDayAttendance.setMustNum(type * 2);
            if (Integer.parseInt(map.get("firstBeginChecked") + "") == 0) {
                date1 = DateUtils.dateTime("yyyy-MM-dd HH:mm", baseDate + " " + map.get("firstBeginTime"));
            } else {
                date1 = DateUtils.dateTime("yyyy-MM-dd HH:mm", DateUtils.getDifferDay(DateUtils.dateTime("yyyy-MM-dd", baseDate), 1) + " " + map.get("firstBeginTime"));
            }
            if (Integer.parseInt(map.get("firstEndChecked") + "") == 0) {
                date2 = DateUtils.dateTime("yyyy-MM-dd HH:mm", baseDate + " " + map.get("firstEndTime"));
            } else {
                date2 = DateUtils.dateTime("yyyy-MM-dd HH:mm", DateUtils.getDifferDay(DateUtils.dateTime("yyyy-MM-dd", baseDate), 1) + " " + map.get("firstEndTime"));
            }

            if (type > 1) {
                if (Integer.parseInt(map.get("secondBeginChecked") + "") == 0) {
                    date3 = DateUtils.dateTime("yyyy-MM-dd HH:mm", baseDate + " " + map.get("secondBeginTime"));
                } else {
                    date3 = DateUtils.dateTime("yyyy-MM-dd HH:mm", DateUtils.getDifferDay(DateUtils.dateTime("yyyy-MM-dd", baseDate), 1) + " " + map.get("secondBeginTime"));
                }
                if (Integer.parseInt(map.get("secondEndChecked") + "") == 0) {
                    date4 = DateUtils.dateTime("yyyy-MM-dd HH:mm", baseDate + " " + map.get("secondEndTime"));
                } else {
                    date4 = DateUtils.dateTime("yyyy-MM-dd HH:mm", DateUtils.getDifferDay(DateUtils.dateTime("yyyy-MM-dd", baseDate), 1) + " " + map.get("secondEndTime"));
                }


            } else if (type > 2) {
                if (Integer.parseInt(map.get("threeBeginChecked") + "") == 0) {
                    date5 = DateUtils.dateTime("yyyy-MM-dd HH:mm", baseDate + " " + map.get("threeBeginTime"));
                } else {
                    date5 = DateUtils.dateTime("yyyy-MM-dd HH:mm", DateUtils.getDifferDay(DateUtils.dateTime("yyyy-MM-dd", baseDate), 1) + " " + map.get("threeBeginTime"));
                }
                if (Integer.parseInt(map.get("threeEndChecked") + "") == 0) {
                    date6 = DateUtils.dateTime("yyyy-MM-dd HH:mm", baseDate + " " + map.get("threeEndTime"));
                } else {
                    date6 = DateUtils.dateTime("yyyy-MM-dd HH:mm", DateUtils.getDifferDay(DateUtils.dateTime("yyyy-MM-dd", baseDate), 1) + " " + map.get("threeEndTime"));
                }


            } else if (type > 3) {
                if (Integer.parseInt(map.get("fourBeginChecked") + "") == 0) {
                    date7 = DateUtils.dateTime("yyyy-MM-dd HH:mm", baseDate + " " + map.get("fourBeginTime"));
                } else {
                    date7 = DateUtils.dateTime("yyyy-MM-dd HH:mm", DateUtils.getDifferDay(DateUtils.dateTime("yyyy-MM-dd", baseDate), 1) + " " + map.get("fourBeginTime"));
                }
                if (Integer.parseInt(map.get("fourBeginChecked") + "") == 0) {
                    date8 = DateUtils.dateTime("yyyy-MM-dd HH:mm", baseDate + " " + map.get("fourBeginTime"));
                } else {
                    date8 = DateUtils.dateTime("yyyy-MM-dd HH:mm", DateUtils.getDifferDay(DateUtils.dateTime("yyyy-MM-dd", baseDate), 1) + " " + map.get("fourBeginTime"));
                }

            }

            //分上下班两组
            List<MonthlyAttendanceIni> inWorkList = new ArrayList<>();
            List<MonthlyAttendanceIni> outWorkList = new ArrayList<>();


            int flag = 0;
            //长期工通行记录
            for (MonthlyAttendanceIni m : l) {

                //第一次上班正常打卡情况
                if (AttendanceConstants.addressIn.indexOf(m.getAddress()) != -1) {
                    if (DateUtils.dateTime("yyyy-MM-dd HH:mm", m.getDateTime()).getTime() >= date1.getTime() - AttendanceConstants.beforeDateTime
                            && DateUtils.dateTime("yyyy-MM-dd HH:mm", m.getDateTime()).getTime() <= date1.getTime()) {
                        userDayAttendance.setIsAttendance(1);
                        userDayAttendance.setIsCome(1);
                        userDayAttendance.setNormalNum(1 + userDayAttendance.getNormalNum());
                        userDayAttendance.setFirstStatus("正常");
                        userDayAttendance.setFirstBeginTime(m.getDateTime());

                        flag = 1;
                        break;
                    }

                }
            }
            if (flag == 0) {
                //上班异常处理
                userDayAttendance = this.inWork(userDayAttendance, l, map, date1);
                userDayAttendance.setFirstStatus("异常");
                userDayAttendance.setFirstBeginTime(userDayAttendance.getSaveTime());

            }

            flag = 0;
            for (MonthlyAttendanceIni m : l) {
                //第一次下班打卡情况
                if (AttendanceConstants.addressOut.indexOf(m.getAddress()) != -1) {
                    if (DateUtils.dateTime("yyyy-MM-dd HH:mm", m.getDateTime()).getTime() >= date2.getTime()
                            && DateUtils.dateTime("yyyy-MM-dd HH:mm", m.getDateTime()).getTime() <= date2.getTime() + AttendanceConstants.afterDateTime
                    ) {
                        userDayAttendance.setIsAttendance(1);
                        userDayAttendance.setIsCome(1);
                        userDayAttendance.setFiveStatus("正常");
                        userDayAttendance.setNormalNum(1 + userDayAttendance.getNormalNum());
                        userDayAttendance.setFirstEndTime(m.getDateTime());
                        flag = 1;
                        break;
                    }
                }
            }

            if (flag == 0) {
                //下班班异常处理
                userDayAttendance = this.outWork(userDayAttendance, l, map, date2);
                userDayAttendance.setFiveStatus("异常");
                userDayAttendance.setSecondEndTime(userDayAttendance.getSaveTime());

            }


            //一天打卡多次的 分4次 6次 8次
            if (type > 1) {
                flag = 0;
                for (MonthlyAttendanceIni m : l) {
                    //第二次上班正常打卡情况
                    if (AttendanceConstants.addressIn.indexOf(m.getAddress()) > -1) {
                        if (DateUtils.dateTime("yyyy-MM-dd HH:mm", m.getDateTime()).getTime() >= date3.getTime() - AttendanceConstants.beforeDateTime
                                && DateUtils.dateTime("yyyy-MM-dd HH:mm", m.getDateTime()).getTime() <= date3.getTime()) {
                            userDayAttendance.setIsAttendance(1);
                            userDayAttendance.setIsCome(1);
                            userDayAttendance.setNormalNum(1 + userDayAttendance.getNormalNum());
                            userDayAttendance.setSecondStatus("正常");
                            userDayAttendance.setSecondBeginTime(userDayAttendance.getSaveTime());

                            flag = 1;
                            break;
                        }
                    }
                }
                if (flag == 0) {
                    //上班异常处理
                    userDayAttendance = this.inWork(userDayAttendance, l, map, date3);
                    userDayAttendance.setSecondStatus("异常");
                    userDayAttendance.setSecondBeginTime(userDayAttendance.getSaveTime());

                }


                flag = 0;
                //第二次下班打卡情况
                for (MonthlyAttendanceIni m : l) {
                    if (AttendanceConstants.addressOut.indexOf(m.getAddress()) != -1) {
                        if (DateUtils.dateTime("yyyy-MM-dd HH:mm", m.getDateTime()).getTime() >= date4.getTime()
                                && DateUtils.dateTime("yyyy-MM-dd HH:mm", m.getDateTime()).getTime() <= date4.getTime() + AttendanceConstants.afterDateTime
                        ) {
                            userDayAttendance.setIsAttendance(1);
                            userDayAttendance.setIsCome(1);
                            userDayAttendance.setNormalNum(1 + userDayAttendance.getNormalNum());
                            userDayAttendance.setSixStatus("正常");
                            userDayAttendance.setSecondEndTime(m.getDateTime());
                            flag = 1;
                            break;
                        }
                    }
                }

                if (flag == 0) {
                    //下班班异常处理
                    userDayAttendance = this.outWork(userDayAttendance, l, map, date6);
                    userDayAttendance.setSixStatus("异常");
                    userDayAttendance.setSecondEndTime(userDayAttendance.getSaveTime());
                }

            }


            //一天打卡多次的 分4次 6次 8次
            if (type > 2) {
                flag = 0;
                //第三次上班正常打卡情况
                for (MonthlyAttendanceIni m : l) {
                    if (AttendanceConstants.addressIn.indexOf(m.getAddress()) != -1) {
                        if (DateUtils.dateTime("yyyy-MM-dd HH:mm", m.getDateTime()).getTime() >= date5.getTime() - AttendanceConstants.beforeDateTime
                                && DateUtils.dateTime("yyyy-MM-dd HH:mm", m.getDateTime()).getTime() <= date5.getTime()) {
                            userDayAttendance.setIsAttendance(1);
                            userDayAttendance.setIsCome(1);
                            userDayAttendance.setNormalNum(1 + userDayAttendance.getNormalNum());
                            userDayAttendance.setThreeStatus("正常");
                            userDayAttendance.setThreeBeginTime(m.getDateTime());

                            flag = 1;
                            break;
                        }
                    }
                    if (flag == 0) {
                        //上班异常处理
                        userDayAttendance = this.inWork(userDayAttendance, l, map, date5);
                        userDayAttendance.setThreeStatus("异常");
                        userDayAttendance.setThreeBeginTime(userDayAttendance.getSaveTime());
                    }
                }


                flag = 0;
                //第三次下班打卡情况
                for (MonthlyAttendanceIni m : l) {
                    if (AttendanceConstants.addressOut.indexOf(m.getAddress()) != -1) {
                        if (DateUtils.dateTime("yyyy-MM-dd HH:mm", m.getDateTime()).getTime() >= date6.getTime()
                                && DateUtils.dateTime("yyyy-MM-dd HH:mm", m.getDateTime()).getTime() <= date6.getTime() + AttendanceConstants.afterDateTime
                        ) {
                            userDayAttendance.setIsAttendance(1);
                            userDayAttendance.setIsCome(1);
                            userDayAttendance.setNormalNum(1 + userDayAttendance.getNormalNum());
                            userDayAttendance.setSevenStatus("正常");
                            userDayAttendance.setThreeEndTime(m.getDateTime());
                            flag = 1;
                            break;
                        }
                    }
                    if (flag == 0) {
                        //下班班异常处理
                        userDayAttendance = this.outWork(userDayAttendance, l, map, date6);
                        userDayAttendance.setSevenStatus("异常");
                        userDayAttendance.setThreeEndTime(userDayAttendance.getSaveTime());
                    }
                }

            }

            //一天打卡多次的 分4次 6次 8次
            if (type > 3) {
                flag = 0;
                //第四次上班正常打卡情况
                for (MonthlyAttendanceIni m : l) {
                    if (AttendanceConstants.addressIn.indexOf(m.getAddress()) != -1) {
                        if (DateUtils.dateTime("yyyy-MM-dd HH:mm", m.getDateTime()).getTime() >= date7.getTime() - AttendanceConstants.beforeDateTime
                                && DateUtils.dateTime("yyyy-MM-dd HH:mm", m.getDateTime()).getTime() <= date7.getTime()) {
                            userDayAttendance.setIsAttendance(1);
                            userDayAttendance.setIsCome(1);
                            userDayAttendance.setNormalNum(1 + userDayAttendance.getNormalNum());
                            userDayAttendance.setFourStatus("正常");
                            userDayAttendance.setFourBeginTime(m.getDateTime());

                            flag = 1;
                            break;
                        }
                    }
                    if (flag == 0) {
                        //上班异常处理
                        userDayAttendance = this.inWork(userDayAttendance, l, map, date7);
                        userDayAttendance.setFourStatus("异常");
                        userDayAttendance.setFourBeginTime(m.getDateTime());
                    }
                }

                flag = 0;
                //第四次下班打卡情况
                for (MonthlyAttendanceIni m : l) {
                    if (AttendanceConstants.addressOut.indexOf(m.getAddress()) != -1) {
                        if (DateUtils.dateTime("yyyy-MM-dd HH:mm", m.getDateTime()).getTime() >= date8.getTime()
                                && DateUtils.dateTime("yyyy-MM-dd HH:mm", m.getDateTime()).getTime() <= date8.getTime() + AttendanceConstants.afterDateTime
                        ) {
                            userDayAttendance.setIsAttendance(1);
                            userDayAttendance.setIsCome(1);
                            userDayAttendance.setNormalNum(1 + userDayAttendance.getNormalNum());
                            userDayAttendance.setEightStatus("正常");
                            userDayAttendance.setFourEndTime(m.getDateTime());
                            flag = 1;
                            break;
                        }
                    }
                }

                if (flag == 0) {
                    //下班班异常处理
                    userDayAttendance = this.outWork(userDayAttendance, l, map, date8);
                    userDayAttendance.setEightStatus("异常");
                    userDayAttendance.setFourEndTime(userDayAttendance.getSaveTime());
                }
            }

            userDayAttendance.setAbnormalNum(userDayAttendance.getLate() + userDayAttendance.getGenerallyLate()
                    + userDayAttendance.getSeriouslyLate() + userDayAttendance.getLeaveEarly()
                    + userDayAttendance.getAbsenteeism());
            resultList.add(userDayAttendance);

        }


        List<Map<String, Object>> resultGroupList = new ArrayList<>();

        Set<String> groupSet = new HashSet<>();
        for (UserDayAttendance u : resultList) {
            groupSet.add(u.getGroupId() + "");
        }

        for (Map<String, Object> m : groupList) {
            for (String s : groupSet) {
                if (Long.parseLong(s) == Long.parseLong(m.get("groupId") + "")) {
                    resultGroupList.add(m);
                }
            }
        }

        List<UserDayAttendanceSum> userDayAttendanceSumList = new ArrayList<>();
        // List<Map<String, Object>> restList = attendanceStatisticsService.listRestSchedule(fistRecordDateTime);

        for (Map<String, Object> m : resultGroupList) {
            List<UserDayAttendance> userDayAttendances = new ArrayList<>();
            UserDayAttendanceSum userDayAttendanceSum = new UserDayAttendanceSum();
            userDayAttendanceSum.setCreateBy(username);
            userDayAttendanceSum.setDateTime(date);
            userDayAttendanceSum.setGroupId(Long.parseLong(m.get("groupId") + ""));
            userDayAttendanceSum.setGroupName(m.get("groupName") + "");
            for (UserDayAttendance u : resultList) {
                if (u.getGroupId() == Long.parseLong(m.get("groupId") + "")) {
                    //计算汇总值
                    int num = 0;
                    if (u.getLate() > 0) {
                        userDayAttendanceSum.setLate(1 + userDayAttendanceSum.getLate());
                        num = 1;
                    }
                    if (u.getGenerallyLate() > 0) {
                        userDayAttendanceSum.setGenerallyLate(1 + userDayAttendanceSum.getGenerallyLate());
                        num = 1;
                    }
                    if (u.getSeriouslyLate() > 0) {
                        userDayAttendanceSum.setSeriouslyLate(1 + userDayAttendanceSum.getSeriouslyLate());
                        num = 1;
                    }
                    if (u.getLeaveEarly() > 0) {
                        userDayAttendanceSum.setLeaveEarly(1 + userDayAttendanceSum.getLeaveEarly());
                        num = 1;
                    }

                    if (u.getAbsenteeism() > 0) {
                        userDayAttendanceSum.setAbsenteeism(userDayAttendanceSum.getAbsenteeism() + 1);
                        num = 1;
                    }
                    userDayAttendanceSum.setAbnormalAttendance(userDayAttendanceSum.getAbnormalAttendance() + num);
                    userDayAttendances.add(u);
                }
            }

            for (List<UserDayAttendance> l : groupOfList) {
                for (UserDayAttendance u : l) {
                    if (u.getGroupId() == Long.parseLong(m.get("groupId") + "")) {
                        userDayAttendanceSum.setMustAttendance(l.size());
                        break;
                    }
                }

            }

            for (Map<String, Object> map : restList) {
                if (Long.parseLong(map.get("groupId") + "") == Long.parseLong(m.get("groupId") + "")) {

                    userDayAttendanceSum.setRestAttendance(userDayAttendanceSum.getRestAttendance() + 1);
                }
            }
            userDayAttendanceSum.setMustAttendance(mustCome);
            userDayAttendanceSum.setNormalAttendance(userOfList.size() - restList.size());
            userDayAttendanceSum.setUserDayAttendanceList(userDayAttendances);
            userDayAttendanceSumList.add(userDayAttendanceSum);
        }

         resultMap.put("dateTime", fistRecordDateTime);
         resultMap.put("userDayAttendanceSumList", userDayAttendanceSumList);
         this.addUserDayAttendanceSum(resultMap);
        return AjaxResult.success("成功");
    }

        //上班异常
    private UserDayAttendance inWork(UserDayAttendance userDayAttendance, List<MonthlyAttendanceIni> l, Map<String, Object> map, Date date) {
        long late = Integer.parseInt(map.get("late") + "") * 60 * 1000;
        long generallyLate = Integer.parseInt(map.get("generallyLate") + "") * 60 * 1000;
        long seriouslyLate = Integer.parseInt(map.get("seriouslyLate") + "") * 60 * 1000;
        long leaveEarly = Integer.parseInt(map.get("leaveEarly") + "") * 60 * 1000;
        long absenteeism = Integer.parseInt(map.get("absenteeism") + "") * 60 * 1000;

        for (MonthlyAttendanceIni m : l) {
            //上班
            //迟到
            if (AttendanceConstants.addressIn.indexOf(m.getAddress()) > -1) {

                if (DateUtils.dateTime("yyyy-MM-dd HH:mm", m.getDateTime()).getTime() > date.getTime()
                        && DateUtils.dateTime("yyyy-MM-dd HH:mm", m.getDateTime()).getTime() <= date.getTime() + late) {

                    userDayAttendance.setLate(1 + userDayAttendance.getLate());
                    userDayAttendance.setIsAbnormal(1);
                    userDayAttendance.setSaveTime(m.getDateTime());
                    return userDayAttendance;


                } else if (DateUtils.dateTime("yyyy-MM-dd HH:mm", m.getDateTime()).getTime() > date.getTime()
                        && DateUtils.dateTime("yyyy-MM-dd HH:mm", m.getDateTime()).getTime() <= date.getTime() + generallyLate) {

                    userDayAttendance.setGenerallyLate(1 + userDayAttendance.getGenerallyLate());
                    userDayAttendance.setIsAbnormal(1);
                    userDayAttendance.setSaveTime(m.getDateTime());
                    return userDayAttendance;

                } else if (DateUtils.dateTime("yyyy-MM-dd HH:mm", m.getDateTime()).getTime() > date.getTime()
                        && DateUtils.dateTime("yyyy-MM-dd HH:mm", m.getDateTime()).getTime() <= date.getTime() + seriouslyLate) {
                    userDayAttendance.setSeriouslyLate(1 + userDayAttendance.getSeriouslyLate());
                    userDayAttendance.setIsAbnormal(1);
                    userDayAttendance.setSaveTime(m.getDateTime());
                    return userDayAttendance;

                } else if (DateUtils.dateTime("yyyy-MM-dd HH:mm", m.getDateTime()).getTime() > date.getTime()
                        && DateUtils.dateTime("yyyy-MM-dd HH:mm", m.getDateTime()).getTime() <= date.getTime() + absenteeism) {
                    userDayAttendance.setSeriouslyLate(1 + userDayAttendance.getSeriouslyLate());
                    userDayAttendance.setIsAbnormal(1);
                    userDayAttendance.setSaveTime(m.getDateTime());
                    return userDayAttendance;

                }
            }
        }

        userDayAttendance.setAbsenteeism(1 + userDayAttendance.getAbsenteeism());
        userDayAttendance.setIsAbnormal(1);

        return userDayAttendance;


    }

    //下班班异常
    private UserDayAttendance outWork(UserDayAttendance userDayAttendance, List<MonthlyAttendanceIni> l, Map<String, Object> map, Date date) {

        long leaveEarly = Integer.parseInt(map.get("leaveEarly") + "") * 60 * 1000;
        long absenteeism = Integer.parseInt(map.get("absenteeism") + "") * 60 * 1000;

        //上班
        //迟到
        for (MonthlyAttendanceIni m : l) {
            if (AttendanceConstants.addressIn.indexOf(m.getAddress()) > -1) {

                if (DateUtils.dateTime("yyyy-MM-dd HH:mm", m.getDateTime()).getTime() < date.getTime()
                        && DateUtils.dateTime("yyyy-MM-dd HH:mm", m.getDateTime()).getTime() >= date.getTime() - leaveEarly) {

                    userDayAttendance.setLeaveEarly(1 + userDayAttendance.getLeaveEarly());
                    userDayAttendance.setIsAbnormal(1);
                    userDayAttendance.setSaveTime(m.getDateTime());
                    return userDayAttendance;


                } else if (DateUtils.dateTime("yyyy-MM-dd HH:mm", m.getDateTime()).getTime() < date.getTime() - leaveEarly
                        && DateUtils.dateTime("yyyy-MM-dd HH:mm", m.getDateTime()).getTime() <= date.getTime() - absenteeism) {

                    userDayAttendance.setAbsenteeism(1 + userDayAttendance.getAbsenteeism());
                    userDayAttendance.setIsAbnormal(1);
                    userDayAttendance.setSaveTime(m.getDateTime());
                    return userDayAttendance;

                }
            }
        }

            userDayAttendance.setAbsenteeism(1 + userDayAttendance.getAbsenteeism());
            userDayAttendance.setIsAbnormal(1);
            return userDayAttendance;

    }
}
